<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/6/13
 * Time: 11:54
 */

namespace iBrand\Shop\Core\Service;


use iBrand\Component\Purchase\Models\Purchase;
use iBrand\Component\Purchase\Models\StockRecord;
use iBrand\Component\Purchase\Models\TakeStock;
use iBrand\Component\Purchase\Models\Transfer;
use iBrand\Component\Purchase\Repositories\PurchaseItemRepository;
use iBrand\Component\Purchase\Repositories\StockRecordRepository;
use iBrand\Component\Purchase\Repositories\TakeStockItemRepository;
use iBrand\Component\Purchase\Repositories\TransferItemRepository;
use iBrand\Shop\Core\Repositories\O2oGoodsProductsRepository;

class WarehouseService
{

    protected $purchaseItemRepository;
    protected $stockRecordRepository;
    protected $o2oGoodsProductsRepository;
    protected $takeStockItemRepository;
    protected $transferItemRepository;

    public function __construct(PurchaseItemRepository $purchaseItemRepository,
                                StockRecordRepository $stockRecordRepository,
                                O2oGoodsProductsRepository $o2oGoodsProductsRepository,
                                TakeStockItemRepository $takeStockItemRepository,
                                TransferItemRepository $transferItemRepository)
    {
        $this->purchaseItemRepository = $purchaseItemRepository;
        $this->stockRecordRepository = $stockRecordRepository;
        $this->o2oGoodsProductsRepository = $o2oGoodsProductsRepository;
        $this->takeStockItemRepository = $takeStockItemRepository;
        $this->transferItemRepository = $transferItemRepository;
    }

    /**
     * 采购入库写入item数据
     * @param $purchase_id
     * @param $good
     * @param $item_id
     * @param $item_type
     */
    public function addPurchaseItem($purchase_id, $good, $item_id, $item_type)
    {
        $purchaseItem = [
            'purchase_id' => $purchase_id,
            'item_type' => $item_type,
            'item_id' => $item_id,
            'quantity' => $good['quantity'],
            'price' => $good['price'],
            'discount' => $good['discount'],
            'purchase_price' => $good['truePrice'],
            'total' => $good['trueAmount'],
            'meta' => [
                'goods_name' => $good['goods_name'],
                'goods_id' => $good['goods_id'],
                'goods_no' => $good['goods_no'],
                'sku' => $good['currentSku'],
                'color' => $good['color'],
                'color_id' => $good['color_id'],
                'size' => $good['size'],
                'size_id' => $good['size_id'],
            ],
            'goods_id' => $good['goods_id'],
        ];

        $this->purchaseItemRepository->create($purchaseItem);
    }


    /**
     * 写入库存流水
     * @param $shop_id
     * @param $purchase_id
     * @param $good
     * @param $purchase_type
     * @param $item_id
     * @param $item_type
     */
    public function addStockRecord($shop_id, $purchase_id, $good, $purchase_type, $item_id, $item_type)
    {
        $current_stock = $this->stockRecordRepository->getCurrentStock($shop_id, $item_id, $item_type);

        $goods_quantity = $good['quantity'];
        $type = StockRecord::PURCHASE_STOCK;
        if ($purchase_type == Purchase::TYPE_REJECT) {
            $type = StockRecord::PURCHASE_RETURNS;
            $goods_quantity = -$good['quantity'];
        }

        $stock = [
            'shop_id' => $shop_id,
            'item_id' => $item_id,
            'item_type' => $item_type,
            'type' => $type,
            'business_type' => Purchase::class,
            'business_type_id' => $purchase_id,
            'change_count' => $goods_quantity,
            'current_stock' => $current_stock + $goods_quantity,
            'goods_id' => $good['goods_id'],
            'meta' => [
                'goods_name' => $good['goods_name'],
                'goods_id' => $good['goods_id'],
                'goods_no' => $good['goods_no'],
                'sku' => $good['currentSku'],
                'color' => $good['color'],
                'color_id' => $good['color_id'],
                'size' => $good['size'],
                'size_id' => $good['size_id'],
            ],
        ];

        $this->stockRecordRepository->create($stock);
    }


    /**
     * o2oProduct 基础数据、库存数据操作
     * @param $shop_id
     * @param $o2oGoodsId
     * @param $base_goods_id
     * @param $quantity
     * @param $order_type
     * @param $product_id
     * @throws \Prettus\Validator\Exceptions\ValidatorException
     */
    public function addO2oGoodsProduct($shop_id, $o2oGoodsId, $base_goods_id, $quantity, $business_type, $order_type, $product_id)
    {
        $o2oGoodsProduct = $this->o2oGoodsProductsRepository->findWhere([
            'shop_id' => $shop_id,
            'o2o_goods_id' => $o2oGoodsId,
            'goods_id' => $base_goods_id,
            'product_id' => $product_id])->first();

        $goods_quantity = $quantity;

        if (($business_type == 'Purchase' AND $order_type == Purchase::TYPE_REJECT) OR
            ($business_type == 'Transfer' AND $order_type == Transfer::TYPE_OUT)
        ) {
            $goods_quantity = -$quantity;
        }

        if ($o2oGoodsProduct) {
            $o2oGoodsProduct->store_nums = $o2oGoodsProduct->store_nums + $goods_quantity;
            $o2oGoodsProduct->save();
        } else {
            $this->o2oGoodsProductsRepository->create([
                'goods_id' => $base_goods_id,
                'o2o_goods_id' => $o2oGoodsId,
                'shop_id' => $shop_id,
                'product_id' => $product_id,
                'store_nums' => $goods_quantity,
            ]);
        }
    }

    /**
     * 商品盘点，写入盘点item表数据
     * @param $takeStockId
     * @param $good
     * @param $item_id
     * @param $item_type
     */
    public function addTakeStockItem($takeStockId, $good, $item_id, $item_type, $status = 1)
    {
        $difference_count = $good['quantity'] - $good['beforeNumber'];
        $difference_amount = $difference_count * $good['price'];

        $itemData = [
            'take_stock_id' => $takeStockId,
            'item_type' => $item_type,
            'item_id' => $item_id,
            'count' => $good['quantity'],
            'before_count' => $good['beforeNumber'],
            'goods_id' => $good['goods_id'],
            'difference_count' => $difference_count,
            'difference_amount' => $difference_amount,
            'status' => $status,
            'meta' => [
                'goods_name' => $good['goods_name'],
                'goods_id' => $good['goods_id'],
                'goods_no' => $good['goods_no'],
                'sku' => $good['currentSku'],
                'color' => $good['color'],
                'color_id' => $good['color_id'],
                'size' => $good['size'],
                'size_id' => $good['size_id'],
            ],
        ];

        $this->takeStockItemRepository->create($itemData);
    }

    /**
     * 商品盘点库存流水
     * @param $shop_id
     * @param $take_stock_id
     * @param $good
     * @param $item_id
     * @param $item_type
     */
    public function addTakeStockRecord($shop_id, $take_stock_id, $good, $item_id, $item_type)
    {
        $current_stock = $this->stockRecordRepository->getCurrentStock($shop_id, $item_id, $item_type);
        if ($current_stock > $good['quantity']) {
            $change_count = -($current_stock - $good['quantity']);
        } elseif ($current_stock < $good['quantity']) {
            $change_count = ($good['quantity'] - $current_stock);
        } else {
            $change_count = 0;
        }

        $stock = [
            'shop_id' => $shop_id,
            'item_id' => $item_id,
            'item_type' => $item_type,
            'type' => StockRecord::TAKE_STOCK,
            'business_type' => TakeStock::class,
            'business_type_id' => $take_stock_id,
            'change_count' => $change_count,
            'current_stock' => $good['quantity'],
            'goods_id' => $good['goods_id'],
            'meta' => [
                'goods_name' => $good['goods_name'],
                'goods_id' => $good['goods_id'],
                'goods_no' => $good['goods_no'],
                'sku' => $good['currentSku'],
                'color' => $good['color'],
                'color_id' => $good['color_id'],
                'size' => $good['size'],
                'size_id' => $good['size_id'],
            ],
        ];

        $this->stockRecordRepository->create($stock);
    }

    /**
     * 库存调拨,写入item数据
     * @param $transferId
     * @param $good
     * @param $item_id
     * @param $item_type
     */
    public function addTransferItem($transferId, $good, $item_id, $item_type)
    {
        $itemData = [
            'transfer_id' => $transferId,
            'item_type' => $item_type,
            'item_id' => $item_id,
            'count' => $good['quantity'],
            'before_count' => $good['beforeNumber'],
            'goods_id' => $good['goods_id'],
            'total_amount' => $good['price'] * $good['quantity'],
            'meta' => [
                'goods_name' => $good['goods_name'],
                'goods_id' => $good['goods_id'],
                'goods_no' => $good['goods_no'],
                'sku' => $good['currentSku'],
                'color' => $good['color'],
                'color_id' => $good['color_id'],
                'size' => $good['size'],
                'size_id' => $good['size_id'],
            ]
        ];

        $this->transferItemRepository->create($itemData);
    }


    /**
     * 库存调拨，写入库存流水数据
     * @param $transfer
     * @param $good
     * @param $item_id
     * @param $item_type
     */
    public function addTransferStockRecord($transfer, $good, $item_id, $item_type)
    {
        $type = $transfer->type;

        if ($type == 'transfer_out') {  //如果是从门店调出
            $current_shop_id = $transfer->out_shop_id;
            $quantity = -$good['quantity'];
            $this->handleStock($transfer, $good, $current_shop_id, $quantity, $item_id, $item_type);

            /*if ($transfer->in_shop_id != 0) {
                $current_shop_id = $transfer->in_shop_id;
                $this->handleStock($transfer, $good, $current_shop_id, $good['quantity'], $item_id, $item_type);
            }*/

        } else {  //如果是调入
            $current_shop_id = $transfer->in_shop_id;
            $quantity = $good['quantity'];
            $this->handleStock($transfer, $good, $current_shop_id, $quantity, $item_id, $item_type);
        }

    }

    public function handleStock($transfer, $good, $current_shop_id, $quantity, $item_id, $item_type)
    {
        $current_stock = $this->stockRecordRepository->getCurrentStock($current_shop_id, $item_id, $item_type);

        $stock = [
            'shop_id' => $current_shop_id,
            'item_id' => $item_id,
            'item_type' => $item_type,
            'type' => StockRecord::STORE_TRANSFER,
            'business_type' => Transfer::class,
            'business_type_id' => $transfer->id,
            'change_count' => $quantity,
            'current_stock' => $current_stock + $quantity,
            'goods_id' => $good['goods_id'],
            'meta' => [
                'goods_name' => $good['goods_name'],
                'goods_id' => $good['goods_id'],
                'goods_no' => $good['goods_no'],
                'sku' => $good['currentSku'],
                'color' => $good['color'],
                'color_id' => $good['color_id'],
                'size' => $good['size'],
                'size_id' => $good['size_id']
            ]
        ];

        $this->stockRecordRepository->create($stock);
    }
}